var objPost;	
if(window.XMLHttpRequest){objPost = new XMLHttpRequest();}
	else{objPost = new ActiveXObject("Microsoft.XMLHTTP");}	
	var CurrentUser=getUserID();
function AddSubjectProcessing(){
	CurrentUser=getUserID();
		var SubjectID = document.getElementById("txtSubjectID").value;
		var SubjectName = document.getElementById("txtSubjectName").value;
		var SubjectShortName = document.getElementById("txtSubjectShortName").value;
		var SubjectCredits = document.getElementById("txtSubjectCredits").value;
		if(SubjectID == "" || SubjectName == "" || SubjectShortName == "" ||SubjectCredits=="")	
		{
			alert("Please fill all the informations");
			return;
		}
		var urlPath = "./rest/subject/create/"+SubjectID+"/"+SubjectName+"/"+SubjectShortName+"/"+SubjectCredits+"/"+CurrentUser+"/";
		//alert(urlPath);
		objPost.onreadystatechange = function(){
			//alert(objPost.status); 
			if (objPost.readyState == 4) {
			        if (objPost.status == 200) {
			        	if (window.confirm("Successfull! Would you like to add more Subject?")){
			        		ClearSubjectsObject();
			        	}else
			        		window.location.href = 'SubjectList.html';
			        	
			        }
			        else{alert("Error: Pls try again!"+objPost.responseText+" --- ");}	
			   }
		};		
		objPost.open("GET",urlPath,true);
		objPost.send(null);
	}
function UpdateSubjectProcessing(){
		var SubjectID = document.getElementById("txtSubjectID").value;
		var SubjectName = document.getElementById("txtSubjectName").value;
		var SubjectShortName = document.getElementById("txtSubjectShortName").value;
		var SubjectCredits = document.getElementById("txtSubjectCredits").value;
		if(SubjectID == "" || SubjectName == "" || SubjectShortName == "")	
		{
			alert("Please fill all the informations");
			return;
		}
		var urlPath = "./rest/subject/update/"+SubjectID+"/"+SubjectName+"/"+SubjectShortName+"/"+SubjectCredits+"/";
		objPost.onreadystatechange = function(){
			if (objPost.readyState == 4) {
			        if (objPost.status == 200) {
			        	alert("Successfull");
			        	window.location.href = 'SubjectList.html';
			        }
			        else{alert("Error: Pls try again!"+objPost.responseText+" --- ");}	
			   }
		};		
		objPost.open("GET",urlPath,true);
		objPost.send(null);
}
function AddTopicProcessing(){
	
	var SubjectID = document.getElementById("ddl_SubjectList").options[document.getElementById('ddl_SubjectList').selectedIndex].value;
	var TopTopicID = document.getElementById("ddl_TopicList").options[document.getElementById('ddl_TopicList').selectedIndex].value;
	var TopicName = document.getElementById("txtTopicName").value;
	var TopicNote = document.getElementById("txtTopicNote").value;
	//alert(TopTopicID);
	if(SubjectID == "" || TopTopicID == "" || TopicName == ""|| TopicNote == ""){
		alert("Please fill all the informations");
		return;
	}
	//alert(TopTopicID);
	var urlPath = "./rest/topic/create/"+TopicName+"/"+TopTopicID+"/"+TopicNote+"/"+SubjectID;
		objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	alert("Successfull!");
		        	//alert(obj.responseText);
		        	window.location.href = 'TopicList.html?SubjectID='+SubjectID;
		        	}
		        else{alert("Error: Pls try again!"+objPost.responseText+" --- ");}	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}

function UpdateTopicProcessing(){
	//alert('aare');
	var TopicID=gup("TopicID");
	var SubjectID=gup("SubjectID");
//	var SubjectID = document.getElementById("ddl_SubjectList").options[document.getElementById('ddl_SubjectList').selectedIndex].value;
	var TopTopicID = document.getElementById("ddl_TopicList").options[document.getElementById('ddl_TopicList').selectedIndex].value;
	var TopicName = document.getElementById("txtTopicName").value;
	var TopicNote = document.getElementById("txtTopicNote").value;
	//var LecturerID=getUserID();	
	//alert(TopTopicID);
	if(  TopicName == ""|| TopicNote == ""){
		alert("Please fill all the informations");
		return;
	}
	
	var urlPath = "./rest/topic/update/"+TopicID+"/"+TopicName+"/"+TopTopicID+"/"+TopicNote+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	alert("Successfull");
		        	window.location.href = 'TopicList.html?SubjectID='+SubjectID;
		        }
		        else{alert("Error: Pls try again!"+objPost.responseText+" --- ");}	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}


function AddQuestionProcessing(){
	CurrentUser=getUserID();
	var topicID = gup("TopicID");
	var topicName = gup("Topic");
	var questionDesc = document.getElementById("txtQuestionDesc").value;
	var questionType="";// = document.getElementById("txtSubjectName").value;
	var questionLevel = document.getElementById("txtQuestionLevel").value;
	if (document.getElementById("rdtMultChoice").checked)
		questionType=1;
	else if (document.getElementById("rdtFillBlank").checked)
		questionType=2;
	else if (document.getElementById("rdtMatching").checked)
		questionType=3;
	else if (document.getElementById("rdtFullText").checked)
		questionType=4;
	
	//alert(questionType);
	if(questionDesc == "" || topicID == "" || questionLevel == "")	
	{
		alert("Please fill all the informations");
		return;
	}
	var urlPath = "./rest/question/create/"+CurrentUser+"/"+questionDesc+"/"+questionType+"/"+questionLevel+"/"+topicID;
	objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
//		        	if (window.confirm("Successfull! Would you like to add more Subject?")){
//		        		ClearSubjectsObject();
//		        	}else
		        		alert('Successfull');
		        		window.location.href = 'QuestionList.html?TopicID='+topicID+'&Topic='+topicName;
		        	//alert(objPost.responseText);
		        }
		        else{alert("Error: Pls try again!"+objPost.responseText+" --- ");}	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}
function UpdateQuestionProcessing(){
	var topicID = gup("TopicID");
	CurrentUser=getUserID();	
	var questionID = document.getElementById("txtQuestionID").value;
	var questionDesc = document.getElementById("txtQuestionDesc").value;
	var questionType="";// = document.getElementById("txtSubjectName").value;
	var questionLevel = document.getElementById("txtQuestionLevel").value;
	if (document.getElementById("rdtMultChoice").checked)
		questionType=1;
	else if (document.getElementById("rdtFillBlank").checked)
		questionType=2;
	else if (document.getElementById("rdtMatching").checked)
		questionType=3;
	else if (document.getElementById("rdtFullText").checked)
		questionType=4;
	if(questionDesc == "" || topicID == "" || questionLevel == "")	
	{
		alert("Please fill all the informations");
		return;
	}
	var urlPath = "./rest/question/update/"+questionID+"/"+CurrentUser+"/"+questionDesc+"/"+questionType+"/"+questionLevel;
	objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	alert("Successfull");
		        	window.location.href = 'QuestionList.html?TopicID='+topicID+'&Topic='+document.getElementById("txtTopic").value;
		        }
		        else{alert("Error: Pls try again!"+objPost.responseText+" --- ");}	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}
function DeleteTopic(TopicID){
	if (window.confirm("Are you sure to delete Topic have ID: "+TopicID+" ?")){
	var urlPath = "./rest/topic/delete/"+TopicID+"/";
	
	obj.onreadystatechange = function(){		
		if (obj.readyState == 4) {
			//alert(urlPath);
		        if (obj.status == 200) {
		        	location.reload(); 
		        }
		        else{alert("Error: Pls try again!"+obj.responseText+" --- ");}	
		   }
		
	};		
	obj.open("GET",urlPath,true);
	obj.send(null);}
	else
		return;
}	
function DeleteQuestion(QuestionID){
	if (window.confirm("Are you sure to delete Question have ID: "+QuestionID+" ?")){
	var urlPath = "./rest/question/delete/"+QuestionID+"/";
	
	obj.onreadystatechange = function(){		
		if (obj.readyState == 4) {
			//alert(urlPath);
		        if (obj.status == 200) {
		        	location.reload(); 
		        }
		        else{alert("Error: Pls try again!"+obj.responseText+" --- ");}	
		   }
		
	};		
	obj.open("GET",urlPath,true);
	obj.send(null);}
	else
		return;
}	
